import { ccs } from "../../../util/services";
import { ItemBase } from "../Item/ItemBase";
import { ItemFactory } from "../Item/itemFactory";

export interface BagData {
    buyNum: number, //购买的数量
    itemCount: number,
    items: any[]
}
export class BagBase<T extends ItemBase> {
    // 背包数据
    bag: BagData = {buyNum: 0, items: [], itemCount: 0};
     // 放置物品
     protected _items = {};
     // 物品分类
     protected _itemTypes = {};
     // 背包容量
    get capacity() {
        return this.bag.itemCount;
    }
    set capacity(value:number) {
        this.bag.itemCount = value;
    }
    // 最大容量(null表示无限容量)
    maxCapacity: number = null;
    buyNum: number = 100; //购买的背包容量
    // 背包类型
    bagType: string = null;

    constructor(bag: BagData, itemTypes: string[], bagType: string) {
        if(!bag) {
            return ;
        }
        this.bagType = bagType;
        for(let key in itemTypes) {
            this._itemTypes[itemTypes[key]] = [];
        }
        this.buyNum = bag.buyNum;
        
        this.bag = bag;

    }
    //获得所有的
    getItems() {
        return this.bag.items;
    }
    //读取
    loadItems(bag) {
        this.bag.items = [];
        bag.bag.items.forEach(val => {
            this._items[val.id] = ItemFactory.create(bag, val);
            this.bag.items.push(val);
        })
    }
    getMaxCapacity() {
        return this.buyNum + this.maxCapacity;
    }

    getCapacity() {
        return this.capacity;
    }
    getItem(key:string|number): T {
        return this._items[key];
    }

    // 添加物品
    addItems(items: any[]) {
        for(let i = 0, len = items.length; i < len; i++) {
            this.addItem(items[i]);
        }
    }

    getItemCount(key:string|number) {
        let pItem = this.getItem(key);
        if(pItem) {
            return pItem.count;
        }
        return 0;
    }
    // 添加物品
    addItem(item: T, count?: number) {
        // if(this.maxCapacity && this.capacity >= this.maxCapacity) {
        //     ccs.winMgr.showWin("toast", { msg: "堆不下了，需要更大的空间" });
        //     return 

        // }
        if(count == undefined) {
            count = item.count;
        }

        let addCount = count || item.count || 1;
        let key = ccs.gf.getItemKey(item.data);

        if(!key) {
            return ;
        }
        let oldCount = 0;
        let pItem = this.getItem(key);
        if(pItem) {
            oldCount = pItem.count;
            pItem.count += addCount;
        } else {
            let tempItem = null;
            if(item instanceof ItemBase) {
                tempItem = item.data;
                pItem = item;
            } 
            this.bag.items.push(tempItem);
            this._items[key] = pItem;
            if(pItem.data.type) {
                this._itemTypes[pItem.data.type].push(pItem);
            }
        }
        // 设置所在的背包类型
        pItem.bagType = this.bagType;
        // this.capacity += count;

    }
    // 删除物品
    delItem(key:number|string, count:number = 1) {
        let pItem = this.getItem(key);
        if(pItem) {
            pItem.count -= count;
            if(pItem.count <= 0) {
                // this.capacity -= 1;
                pItem.count = 0;
                // let list = this.getItemsByTypeEx(pItem.data.type);
                this.bag.items.splice(ccs.gf.findListIdxById(this.bag.items, key), 1);
                // list.splice(ccs.gf.findListIdx(list, key), 1);
                this._items[key] = null;
            }
        }
    }
    fixItem(key: number | string, data: any) {

    }
    getItemsByTypeEx(type:string, defaultList?:any[]):any[] {
        return this._itemTypes[type] || defaultList;
    }


}